package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerActivityDetailsComponent;
import com.kucai.yifaqapp.dagger.module.activity.ActivityDetailsModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResGuanfang;
import com.kucai.yifaqapp.model.network.entity.res.ResGuanfang2;
import com.kucai.yifaqapp.model.network.entity.res.Resdraw;
import com.kucai.yifaqapp.presenter.activity.ActivityDetailsPresenter;
import com.kucai.yifaqapp.ui.view.ActivityDetailsView;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.widget.Activity_Zhongjiang;
import com.kucai.yifaqapp.widget.LuckSpan;

import java.util.ArrayList;

public class ActivityDetails extends BaseActivity<ActivityDetailsPresenter> implements ActivityDetailsView {

    TextView titleLeftTv;
    TextView headerTitle;
    TextView qiandaotext;
    RecyclerView activty1_rl;
    RecyclerView activty2_rl;
    RecyclerView activty3_rl;
    ScrollView qiandaosongli;
    ImageView topimg1;
    TextView activitytime;
    TextView canyutext;
    TextView contitents;
    TextView lunpancishu;
    ImageView qiandaobt;
    ImageView qiandaobt1;
    ImageView qiandaobt2;
    //    ImageView denglubt;
    LuckSpan ls_lucky;
    ImageView iv_start;
    ScrollView denglusongli;
    private String id;
    private String imgid;
    private String type;
    private ArrayList<ResGuanfang.DataBean.RuleBean> rulebean = new ArrayList<>();
    private ArrayList<ResGuanfang.DataBean.PrizeBean> prizeBean = new ArrayList<>();
    private ArrayList<ResGuanfang2.DataBean.PrizeBean> prizeBean2 = new ArrayList<>();//登录抽奖
    private Adapter adapter;
    private Adapter1 adapter1;
    private Adapter2 adapter2;
    private boolean mIsClickStart; //默认为false避免还没点击开始转动就会提示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        qiandaotext = findViewById(R.id.qiandaotext);
        activty1_rl = findViewById(R.id.activty1_rl);
        lunpancishu = findViewById(R.id.lunpancishu);
        activty2_rl = findViewById(R.id.activty2_rl);//奖品
        activty3_rl = findViewById(R.id.activty3_rl);//奖品
        qiandaosongli = findViewById(R.id.qiandaosongli);
        topimg1 = findViewById(R.id.topimg1);


        activitytime = findViewById(R.id.activitytime);
        canyutext = findViewById(R.id.canyutext);
        qiandaobt = findViewById(R.id.qiandaobt);
        qiandaobt1 = findViewById(R.id.qiandaobt1);
        qiandaobt2 = findViewById(R.id.qiandaobt2);
//        denglubt = findViewById(R.id.denglubt);
        ls_lucky = findViewById(R.id.ls_lucky);
        iv_start = findViewById(R.id.iv_start);
        contitents = findViewById(R.id.contitents);
        denglusongli = findViewById(R.id.denglusongli);
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        imgid = intent.getStringExtra("doc");
        type = intent.getStringExtra("activitytype");

        if (type.equals("1")) {
            qiandaosongli.setVisibility(View.VISIBLE);
            headerTitle.setText("签到好礼");
            mPresenter.getguanfangactivitys(id, imgid, Info.user.token);
        } else {
            //接口的坑，重新写一遍
            mPresenter.getguanfangactivitys2(id, imgid, Info.user.token);
            denglusongli.setVisibility(View.VISIBLE);
            headerTitle.setText("转盘抽奖");
        }
        activty1_rl.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
        activty1_rl.setAdapter(adapter);

        activty2_rl.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter1 = new Adapter1();
        activty2_rl.setAdapter(adapter1);


        activty3_rl.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter2 = new Adapter2();
        activty3_rl.setAdapter(adapter2);


    }

    @Override
    protected int setContentView() {
        return R.layout.activity_details;
    }

    @Override
    protected void ChangeSkin() {
    }

    @Override
    protected void initDagger() {
        DaggerActivityDetailsComponent.builder()
                .activityDetailsModule(new ActivityDetailsModule(this))
                .build().inject(this);
    }

    @Override
    public void getguanfang(final ResGuanfang resActivitys) {
        try {
            //签到
            if (type.equals("1")) {
                rulebean.clear();
                rulebean.addAll(resActivitys.data.rule);
                adapter.notifyDataSetChanged();
                qiandaotext.setText("我已累计签到" + resActivitys.data.continueDays + "天");
                if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {
                    contitents.setText("我的次数:1");
                } else {
                    contitents.setText("我的次数:0");
                }
                prizeBean.clear();
                prizeBean.addAll(resActivitys.data.prize);
                adapter1.notifyDataSetChanged();
                qiandaobt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {
                            mPresenter.getdraw(id, Info.user.token);
                        } else {
                            if (resActivitys.data.is_drew.equals("0")) {
                                Toast.makeText(ActivityDetails.this, "您已经抽过奖了", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ActivityDetails.this, "您未满足抽奖条件", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
                qiandaobt1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {

                            mPresenter.getdraw(id, Info.user.token);

                        } else {
                            if (resActivitys.data.is_drew.equals("0")) {
                                Toast.makeText(ActivityDetails.this, "您已经抽过奖了", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ActivityDetails.this, "您未满足抽奖条件", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
                qiandaobt2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {
                            mPresenter.getdraw(id, Info.user.token);
                        } else {
                            if (resActivitys.data.is_drew.equals("0")) {
                                Toast.makeText(ActivityDetails.this, "您已经抽过奖了", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ActivityDetails.this, "您未满足抽奖条件", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void getguanfang2(final ResGuanfang2 resActivitys) {
//        SYSOUtils.sOut_("返回信息" + resActivitys);
        try {
            prizeBean2.clear();
            prizeBean2.addAll(resActivitys.data.prize);
            adapter2.notifyDataSetChanged();
            if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {
                lunpancishu.setText("我的次数:1");
            } else {
                lunpancishu.setText("我的次数:0");
            }
            activitytime.setText(resActivitys.data.start_time + "至" + resActivitys.data.end_time);
            canyutext.setText(resActivitys.data.content);
            iv_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (resActivitys.data.is_join.equals("1") && resActivitys.data.is_drew.equals("1")) {
                        Toast.makeText(ActivityDetails.this, "满足抽奖条件", Toast.LENGTH_SHORT).show();
                        mPresenter.getdraw(id, Info.user.token);
                    } else {
                        if (resActivitys.data.is_drew.equals("0")) {
                            Toast.makeText(ActivityDetails.this, "您已经抽过奖了", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ActivityDetails.this, "您未满足抽奖条件", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        } catch (Exception e) {

        }
    }

    @Override
    public void getdraw(Resdraw resdraw) {
        try {
            lunpancishu.setText("我的次数:0");
            contitents.setText("我的次数:0");
            if (type.equals("1")) {
                Toast.makeText(ActivityDetails.this, "恭喜获得" + resdraw.level + "等奖" +
                        resdraw.bonus + "元", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(ActivityDetails.this, Activity_Zhongjiang.class);
                intent.putExtra("level", resdraw.level + "");
                intent.putExtra("bonus", resdraw.bonus + "");
                startActivity(intent);
            } else {
                //判断播放那个轮盘抽奖的动画
                if (resdraw.bonusName.equals("谢谢参与")) {

                    //传入的参数由后台返回指定中哪个奖项
                    ls_lucky.luckyStart(2);
                    //逐渐停止转盘
                    ls_lucky.luckStop();
                    Intent intent = new Intent(ActivityDetails.this, Activity_Zhongjiang.class);
                    startActivity(intent);
                } else {
                    //传入的参数由后台返回指定中哪个奖项
                    ls_lucky.luckyStart(Integer.parseInt(resdraw.level));
                    //逐渐停止转盘
                    ls_lucky.luckStop();


                    ls_lucky.setOnSpanRollListener(new LuckSpan.SpanRollListener() {
                        @Override
                        public void onSpanRollListener(double speed) {
                            if (0 == speed) {
                                //已经停止下来了 提示中奖名并释放按钮
                                if (mIsClickStart) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Intent intent = new Intent(ActivityDetails.this, Activity_Zhongjiang.class);
                                            intent.putExtra("level", resdraw.level + "");
                                            intent.putExtra("bonus", resdraw.bonus + "");
                                            startActivity(intent);
                                            mIsClickStart = false;
                                        }
                                    });
                                }
                            }
                        }
                    });
                }

            }
        } catch (Exception e) {

        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(ActivityDetails.this)
                    .inflate(R.layout.text_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {

            holder.textid.setText("签到第" + rulebean.get(position).days + "天:充值" + formateRate(rulebean.get(position).pz.money)
                    + "投注量" + rulebean.get(position).pz.bet + "获得1次抽奖机会");
        }

        @Override
        public int getItemCount() {
            return rulebean.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView textid;

            MViewHolder(View itemView) {
                super(itemView);
                textid = (TextView) itemView.findViewById(R.id.textid);


            }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(ActivityDetails.this)
                    .inflate(R.layout.text_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            if (prizeBean.get(position).pz.size() == 3) {
                holder.textid.setText("第" + prizeBean.get(position).days + "天奖品：" + prizeBean.get(position).pz.get(0).bonus + "、"
                        + prizeBean.get(position).pz.get(1).bonus + "、" + prizeBean.get(position).pz.get(2).bonus);
            }
            if (prizeBean.get(position).pz.size() == 2) {
                holder.textid.setText("第" + prizeBean.get(position).days + "天奖品：" + prizeBean.get(position).pz.get(0).bonus + "、"
                        + prizeBean.get(position).pz.get(1).bonus);
            }
            if (prizeBean.get(position).pz.size() == 1) {
                holder.textid.setText("第" + prizeBean.get(position).days + "天奖品：" + prizeBean.get(position).pz.get(0).bonus);
            }

        }

        @Override
        public int getItemCount() {
            return prizeBean.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView textid;

            MViewHolder(View itemView) {
                super(itemView);
                textid = (TextView) itemView.findViewById(R.id.textid);


            }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Adapter2 extends RecyclerView.Adapter<Adapter2.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(ActivityDetails.this)
                    .inflate(R.layout.text_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.textid.setTextColor(getResources().getColor(R.color.settingbg));
            holder.textid.setText(prizeBean2.get(position).days + "等奖:" + prizeBean2.get(position).pz.bonus);
        }

        @Override
        public int getItemCount() {
            return prizeBean2.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView textid;

            MViewHolder(View itemView) {
                super(itemView);
                textid = (TextView) itemView.findViewById(R.id.textid);


            }
        }
    }

    public String formateRate(String rateStr) {
        if (rateStr.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            //找到小数点在字符串中的位置,找到返回一个int类型的数字,不存在的话返回 -1
            num = rateStr.indexOf(".");

            String dianAfter = rateStr.substring(0, num + 1);//输入100.30,dianAfter = 100.
            String afterData = rateStr.replace(dianAfter, "");//把原字符(rateStr)串包括小数点和小数点前的字符替换成"",最后得到小数点后的字符(不包括小数点)

            //判断小数点后字符的长度并做不同的操作,得到小数点后两位的字符串
            if (afterData.length() < 2) {
                afterData = afterData + "0";
            } else {
                afterData = afterData;
            }
            //返回元字符串开始到小数点的位置 + "." + 小数点后两位字符
            return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
        } else {
            if (rateStr == "1") {
                return "100";
            } else {
                return rateStr;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //避免轮盘没有停止，造成崩溃
        ls_lucky.luckStop();
    }
}
